﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Daidalos.Utilities;

namespace Daidalos.Graphics
{
    /// <summary>
    ///		Encapsulates a Direct3D texture object.
    /// </summary>
    public class Texture
    {

        #region Attributes
        /// <summary>
        ///		Keep track of the texture objects
        ///		currently bound to the texture units.
        /// </summary>
        private static Texture[] activeTextures = new Texture[8];

        /// <summary>
        ///		The Direct3D texture object.
        /// </summary>
        private Microsoft.DirectX.Direct3D.Texture data = null;
        #endregion

        #region Properties
        /// <summary>
        ///		Implement accessors to the encapsulated
        ///		texture object.
        /// </summary>
        public Microsoft.DirectX.Direct3D.Texture Data
        {
            get
            {
                return data;
            }
        }
        #endregion

		#region Constructors
		/// <summary>
        ///		Creates a texture object from a given Direct3D texture.
        /// </summary>
        /// <param name="data">
		///		The Direct3D texture object.
		/// </param>
        public Texture(Microsoft.DirectX.Direct3D.Texture data)
        {
          this.data = data;
		}

		/// <summary>
		///		Releases AGP memory associated with the texture
		///		instance.
		/// </summary>
		~Texture()
		{
			// Remove the texture data from the AGP memory.
			if (data != null && !data.Disposed)
			{
				data.Dispose();
				data = null;
			}
		}
		#endregion

		#region Methods
		/// <summary>
		///		Binds the texture to the specified texture unit.
        /// </summary>
        /// <param name="stage">
		///		The target texture unit.
		/// </param>
        public void Bind(int unit)
        {
            // Use lazy texture binding.
            if (activeTextures[unit] != this)
            {
                activeTextures[unit] = this;
                Graphics.RenderDevice.Instance.Device.SetTexture(unit, data);
            }
        }

		/// <summary>
		///		This method frees resources allocated by
		///		the texture object.
		/// </summary>
		public void Dispose()
		{
			if (data != null && !data.Disposed)
			{
				data.Dispose();
				data = null;
			}
		}
        #endregion

    }
}
